import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { $t } from '#/locales';
import { z } from '#/adapter/form';
import { getSimpleDictTypeList } from '#/api/system/dict/type';

// ============================== 字典类型 ==============================

/** 类型新增/修改的表单 */
export function useTypeFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.system.dict.type.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.dict.type.namePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'type',
      label: $t('views.system.dict.type.type'),
      component: 'Input',
      componentProps: (values) => {
        return {
          placeholder: $t('views.system.dict.type.typePlaceholder'),
          disabled: !!values.id,
        };
      },
      rules: 'required',
      dependencies: {
        triggerFields: [''],
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
      },
    },
  ];
}

/** 类型列表的搜索表单 */
export function useTypeGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.system.dict.type.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.dict.type.namePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'type',
      label: $t('views.system.dict.type.type'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.dict.type.typePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: $t('common.statusPlaceholder'),
        allowClear: true,
      },
    },
  ];
}

/** 类型列表的字段 */
export function useTypeGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: $t('views.system.dict.type.id'),
      minWidth: 100,
    },
    {
      field: 'name',
      title: $t('views.system.dict.type.name'),
      minWidth: 200,
    },
    {
      field: 'type',
      title: $t('views.system.dict.type.type'),
      minWidth: 220,
    },
    {
      field: 'status',
      title: $t('common.status'),
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'remark',
      title: $t('common.remark'),
      minWidth: 180,
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      minWidth: 120,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

// ============================== 字典数据 ==============================

// TODO @芋艿：后续针对 antd，增加
/**
 * 颜色选项
 */
const colorOptions = [
  { value: '', label: $t('views.system.dict.data.colorOptions.none') },
  { value: 'processing', label: $t('views.system.dict.data.colorOptions.processing') },
  { value: 'success', label: $t('views.system.dict.data.colorOptions.success') },
  { value: 'default', label: $t('views.system.dict.data.colorOptions.default') },
  { value: 'warning', label: $t('views.system.dict.data.colorOptions.warning') },
  { value: 'error', label: $t('views.system.dict.data.colorOptions.error') },
  { value: 'pink', label: 'pink' },
  { value: 'red', label: 'red' },
  { value: 'orange', label: 'orange' },
  { value: 'green', label: 'green' },
  { value: 'cyan', label: 'cyan' },
  { value: 'blue', label: 'blue' },
  { value: 'purple', label: 'purple' },
];

/** 数据新增/修改的表单 */
export function useDataFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'dictType',
      label: $t('views.system.dict.data.dictType'),
      component: 'ApiSelect',
      componentProps: (values) => {
        return {
          api: getSimpleDictTypeList,
          placeholder: $t('views.system.dict.data.dictTypePlaceholder'),
          labelField: 'name',
          valueField: 'type',
          disabled: !!values.id,
        };
      },
      rules: 'required',
      dependencies: {
        triggerFields: [''],
      },
    },
    {
      fieldName: 'label',
      label: $t('views.system.dict.data.label'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.dict.data.labelPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'value',
      label: $t('views.system.dict.data.value'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.dict.data.valuePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'sort',
      label: $t('views.common.sort'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.common.sortPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: $t('common.statusPlaceholder'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
    {
      fieldName: 'colorType',
      label: $t('views.system.dict.data.colorType'),
      component: 'Select',
      componentProps: {
        options: colorOptions,
        placeholder: $t('views.system.dict.data.colorTypePlaceholder'),
      },
    },
    {
      fieldName: 'cssClass',
      label: $t('views.system.dict.data.cssClass'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.dict.data.cssClassPlaceholder'),
      },
      help: $t('views.system.dict.data.cssClassHelpMessage'),
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
      },
    },
  ];
}

/** 字典数据列表搜索表单 */
export function useDataGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'label',
      label: $t('views.system.dict.data.label'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.dict.data.labelPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: $t('common.statusPlaceholder'),
        allowClear: true,
      },
    },
  ];
}

/** 字典数据表格列 */
export function useDataGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: $t('views.system.dict.data.id'),
      minWidth: 100,
    },
    {
      field: 'label',
      title: $t('views.system.dict.data.label'),
      minWidth: 180,
    },
    {
      field: 'value',
      title: $t('views.system.dict.data.value'),
      minWidth: 100,
    },
    {
      field: 'sort',
      title: $t('views.common.sort'),
      minWidth: 100,
    },
    {
      field: 'status',
      title: $t('common.status'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'colorType',
      title: $t('views.system.dict.data.colorType'),
      minWidth: 120,
      slots: { default: 'colorType' },
    },
    {
      field: 'cssClass',
      title: $t('views.system.dict.data.cssClass'),
      minWidth: 120,
      slots: { default: 'cssClass' },
    },
    {
      title: $t('common.createTime'),
      field: 'createTime',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      minWidth: 120,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
